﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Sio.Mdm.BF.Idm;
using Sio.Mdm.Types.Database;
using Ext.Net;
using Sio.Mdm.Entities;
using Sio.Mdm.Client;
using Sio.Mdm.BF.Sifranti;
using Sio.Mdm.Types;
using System.Collections.Generic;
using Sio.Mdm.Web.Controls;
using Sio.Mdm.BF.Administracija;
using System.Text;
using System.IO;

namespace Sio.Mdm.Web.Uporabnik
{
    public partial class Spremenigeslo : Sio.Mdm.Web.Controls.Page
    {
        #region properties
        private int IdmStevilka
        {
            get
            {
                try
                {
                    var sv = Request.GetValue<string>("Idm");
                    if (sv == null)
                        return UserSession.IdmStevilka;
                    else
                        return Request.GetValue<int>("Idm");
                }
                catch
                {
                    return -2;
                }
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (UserSession.SuperAdmin)
            {//brez veze da forcam staro geslo... itak potem preverjam če ustreza...
                tfStaroGeslo.MinLength = 0;
                tfStaroGeslo.AllowBlank = true;
            }
        }

        protected void SpremeniGesloButton(object sender, DirectEventArgs e)
        {
          Sio.Mdm.Web.Controls.DialogButtons button =
		      e.DeserializeParameter<Sio.Mdm.Web.Controls.DialogButtons>("buttonType");

	        if (button == DialogButtons.Save)
	        {
            
            string staroGeslo = this.tfStaroGeslo.Text;

            UporabniskiRacun uporabniskiRacun = EntityFactory.LoadEntity<UporabniskiRacun>
                (new Filter<UporabniskiRacun>().Where(r => r.IdmStevilka == IdmStevilka)
            );
          
            List<string> napake = new List<string>();

            if (!UserSession.SuperAdmin)  //root uporabnik lahko vedno spremeni staro geslo
            {
                if (String.IsNullOrEmpty(this.tfStaroGeslo.Text) | (this.tfStaroGeslo.Text.Length <= 3))
                    napake.Add(" - staro geslo");
            }
            if (String.IsNullOrEmpty(this.tfNovoGeslo.Text) | (this.tfNovoGeslo.Text.Length <= 3))
              napake.Add(" - novo geslo");
            if (String.IsNullOrEmpty(this.tfPotrdiGeslo.Text) | (this.tfPotrdiGeslo.Text.Length <= 3))
              napake.Add(" - potrdi geslo");

            var auth = ServiceFactory.GetService<IAvtorizacijaService>();

            if (!UserSession.SuperAdmin)  //root uporabnik lahko vedno spremeni staro geslo
            {
                string staroGesloDecrypted = uporabniskiRacun.Geslo;
                auth.DecryptPassword(ref staroGesloDecrypted);

                if (staroGesloDecrypted != staroGeslo)
                    napake.Add(" - stari gesli se ne ujemata");
            }
  
            if (napake.Count > 0)
            {
              X.Msg.Alert("Manjkajoči podatki", "Popraviti morate še naslednja vnosna polja: <br />" + String.Join("<br />", napake.ToArray())).Show();
              return;
            }
            else
            {
              var geslo = this.tfNovoGeslo.Text;
              auth.EncryptPassword(ref geslo);
              uporabniskiRacun.Geslo = geslo;
              uporabniskiRacun.Update();
            }
            SpremeniGesloProxy.Close();
          }
          if (button == Sio.Mdm.Web.Controls.DialogButtons.Close)
          {
            SpremeniGesloProxy.Close();
          }            
        }
    }
}
